package com.wsc.wisdomchina.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.wsc.wisdomchina.R;
import com.wsc.wisdomchina.javaBean.ItemSectionListBean;

import java.util.List;

/**
 * 功能编辑
 * Created by ${wangtao} on 2017/2/24.
 */

public class EditFunctionAdapter extends BaseAdapter {
    private List<ItemSectionListBean> objects;
    private Context context;
    private LayoutInflater layoutInflater;
    private List<String> idList;

    public EditFunctionAdapter(Context context, List<ItemSectionListBean> objects, List<String> idList) {
        this.objects = objects;
        this.context = context;
        this.idList = idList;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public ItemSectionListBean getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_fourmodule_head, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        initializeViews((ItemSectionListBean) getItem(position), (ViewHolder) convertView.getTag(),position);
        return convertView;
    }

    private void initializeViews(ItemSectionListBean object, ViewHolder holder,final int position) {
        //TODO implement

        if (object != null) {
            Glide.with(context).load(object.getIcon())
                    .placeholder(R.mipmap.own_pic_logout)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(holder.itemHeadImg);
            holder.itemHeadName.setText(object.getName());
        }

        holder.iv_add.setVisibility(View.VISIBLE);
        boolean isExit = false;
        if (idList != null && idList.size() > 0) {
            for (String id : idList) {
                if (id.equals(object.getId())) {
                    isExit = true;
                    break;
                }
            }

        }

        if (isExit) {
            holder.iv_add.setImageResource(R.mipmap.selectreveal);
        } else {
            holder.iv_add.setImageResource(R.mipmap.contactadd);
        }
        holder.iv_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemDeleteListener.onDeleteClick(position);
            }
        });
    }

    /**
     * 删除按钮的监听接口
     */
    public interface onItemDeleteListener {
        void onDeleteClick(int i);
    }

    private onItemDeleteListener mOnItemDeleteListener;

    public void setOnItemDeleteClickListener(onItemDeleteListener mOnItemDeleteListener) {
        this.mOnItemDeleteListener = mOnItemDeleteListener;
    }


    protected class ViewHolder {
        private ImageView itemHeadImg;
        private TextView itemHeadName;
        private ImageView iv_add;

        public ViewHolder(View view) {
            itemHeadImg = (ImageView) view.findViewById(R.id.item_sida_gongnneg_head_img);
            itemHeadName = (TextView) view.findViewById(R.id.item_sida_gongnneg_head_name);
            iv_add = (ImageView) view.findViewById(R.id.iv_add);
        }
    }
}
